package com.gzk.org.service.impl;

import com.gzk.basic.exception.BusinessException;
import com.gzk.basic.service.impl.BaseServiceImpl;
import com.gzk.org.domain.Employee;
import com.gzk.org.domain.Shop;
import com.gzk.org.mapper.EmployeeMapper;
import com.gzk.org.mapper.ShopMapper;
import com.gzk.org.service.IEmployeeService;
import com.gzk.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        //判断两次输入的密码是否一致
        Employee admin = shop.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次输入的密码不一致！！");
        }
        //判断用户是否被注册
        Employee employee = employeeMapper.loadByCondition(admin);
        if(employee!=null){
            throw new BusinessException("该账号已存在，请直接登录！");
        }

        //保存用户
        employeeService.add(admin);

        //设置admin_id
        shop.setAdmin(admin);

        //保存shop
        shopMapper.save(shop);

        //修改店铺id
        admin.setShop(shop);
        employeeMapper.update(admin);
    }
}
